package com.cake.liulanxiangzhu.service.impl;

import com.cake.liulanxiangzhu.exception.ServiceException;
import com.cake.liulanxiangzhu.pojo.entity.ContactInformation;
import com.cake.liulanxiangzhu.pojo.dto.ContactInformationDTO;
import com.cake.liulanxiangzhu.pojo.dto.ContactInformationUpdateDTO;
import com.cake.liulanxiangzhu.pojo.vo.ContactInformationVO;
import com.cake.liulanxiangzhu.mapper.ContactInformationMapper;
import com.cake.liulanxiangzhu.service.ContactInformationService;
import com.cake.liulanxiangzhu.utils.DataUseUtil;
import com.cake.liulanxiangzhu.utils.LoginUtil;
import com.cake.liulanxiangzhu.web.JsonPage;
import com.cake.liulanxiangzhu.web.ProductCode;
import com.cake.liulanxiangzhu.web.ServiceCode;
import io.jsonwebtoken.Claims;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * (ContactInformation)表服务实现类
 *
 * @author dinghao
 * @since 2023-02-21 21:45:42
 */
@Service
@Transactional
public class ContactInformationServiceImpl implements ContactInformationService {
    @Autowired
    private ContactInformationMapper contactInformationMapper;

    @Autowired
    private LoginUtil loginUtil;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ContactInformationVO queryById(Integer id) {
        return this.contactInformationMapper.queryById(id);
    }

    @Override
    public ContactInformationVO queryWxAddress(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        System.out.println("请求头数据："+token);
        if (DataUseUtil.isNotBlank(token)){
            String userId = String.valueOf(loginUtil.parser(token).get("id", Integer.class));
            ContactInformationVO contactInformationVO = contactInformationMapper.queryByUserId(Integer.parseInt(userId));
            return contactInformationVO;
        }else {
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"用户未登录");
        }

    }


    /**
     * 分页查询
     *
     * @param contactInformation 筛选条件
     * @return 查询结果
     */
    @Override
    public JsonPage<ContactInformationVO> queryByPage(ContactInformation contactInformation, Integer page , Integer pageSize) {
        PageHelper.startPage(page,pageSize);

        return null;
    }

    @Override
    public List<ContactInformationVO> queryWxAddressList(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (DataUseUtil.isNotBlank(token)){
            String userId = String.valueOf(loginUtil.parser(token).get("id", Integer.class));
            List<ContactInformationVO> contactInformationVOS = contactInformationMapper.queryByUserIdList(Integer.parseInt(userId));
            return contactInformationVOS;
        }else {
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"用户未登录");
        }
    }

    /**
     * 新增数据
     *
     * @param contactInformationDto 实例对象
     * @return 实例对象
     */
    @Override
    public void insert(HttpServletRequest request, ContactInformationDTO contactInformationDto) {
        String token = request.getHeader("Authorization");
        String userId = loginUtil.parser(token).getId();
        Integer isDefault = contactInformationDto.getIsDefault();
        if (DataUseUtil.isBlank(userId)||DataUseUtil.isBlank(token)){
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"用户未登录");
        }
        if (isDefault==0){
            ContactInformation contactInformation = new ContactInformation();
            BeanUtils.copyProperties(contactInformationDto,contactInformation);
            contactInformation.setUserId(Integer.parseInt(userId));
            contactInformation.setCreatTime(new Date());
            contactInformation.setUpdateTime(new Date());
            contactInformationMapper.insert(contactInformation);
        }else {
            ContactInformationVO contactInformationVO = contactInformationMapper.queryByUserId(Integer.parseInt(userId));
            if (contactInformationVO!=null){
                ContactInformation contactInformationUpdate = new ContactInformation();
                Integer id = contactInformationVO.getId();
                contactInformationUpdate.setId(id);
                contactInformationUpdate.setIsDefault(ProductCode.NO.getCode());
                contactInformationMapper.update(contactInformationUpdate);
            }
            ContactInformation contactInformation = new ContactInformation();
            BeanUtils.copyProperties(contactInformationDto,contactInformation);
            contactInformation.setUserId(Integer.parseInt(userId));
            contactInformation.setCreatTime(new Date());
            contactInformation.setUpdateTime(new Date());
            contactInformationMapper.insert(contactInformation);
        }
    }

    /**
     * 修改数据
     *
     * @param contactInformationUpdateDto 实例对象
     * @return 实例对象
     */
    @Override
    public void update(HttpServletRequest request, ContactInformationUpdateDTO contactInformationUpdateDto) {
        String token = request.getHeader("Authorization");
        String userId = loginUtil.parser(token).getId();
        if (DataUseUtil.isBlank(userId)||DataUseUtil.isBlank(token)){
            throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"用户未登录");
        }
        Integer isDefault = contactInformationUpdateDto.getIsDefault();

        if (isDefault==0){
            ContactInformation contactInformation = new ContactInformation();
            BeanUtils.copyProperties(contactInformationUpdateDto,contactInformation);
            contactInformation.setUserId(Integer.parseInt(userId));
            contactInformation.setUpdateTime(new Date());
            contactInformationMapper.update(contactInformation);
        }else {
            ContactInformationVO contactInformationVO = contactInformationMapper.queryByUserId(Integer.parseInt(userId));
            ContactInformation contactInformationUpdate = new ContactInformation();
            Integer id = contactInformationVO.getId();
            contactInformationUpdate.setId(id);
            contactInformationUpdate.setIsDefault(ProductCode.NO.getCode());
            contactInformationMapper.update(contactInformationUpdate);
            ContactInformation contactInformation = new ContactInformation();
            BeanUtils.copyProperties(contactInformationUpdateDto,contactInformation);
            contactInformation.setUserId(Integer.parseInt(userId));
            contactInformation.setUpdateTime(new Date());
            contactInformationMapper.update(contactInformation);
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public void deleteById(Integer id) {
       if (id==null){
           throw new ServiceException(ServiceCode.ERR_BAD_REQUEST,"未选择要删除的数据");
       }
        contactInformationMapper.deleteById(id);

    }
}
